package cn.zhaoyuening.zshop4j.manager.web;

import cn.zhaoyuening.zshop4j.common.bean.EasyUiTreeNode;
import cn.zhaoyuening.zshop4j.common.bean.ZShop4jResponceResult;
import cn.zhaoyuening.zshop4j.common.utils.JsonUtils;
import cn.zhaoyuening.zshop4j.content.service.ContentCategoryService;
import cn.zhaoyuening.zshop4j.manager.pojo.TbContentCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by Zhao on 2017/4/12.
 */
@Controller
public class ContentCategoryController {
    @Autowired
    private ContentCategoryService contentCategoryService;

    @RequestMapping(value = "contentCategorys",method = RequestMethod.GET,produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String findByParentId(@RequestParam(defaultValue = "0") long id) {
        List<EasyUiTreeNode> list = contentCategoryService.getContentCategoryList(id);
        String jsonInfo = JsonUtils.objectToJson(list);
        return jsonInfo;
    }

    @RequestMapping(value = "contentCategory",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String insert(TbContentCategory contentCategory){
        contentCategory.setIsParent(false);
        contentCategory.setStatus(1);
        contentCategory.setSortOrder(1);
        contentCategory.setUpdated(new Date());
        contentCategory.setCreated(new Date());
        ZShop4jResponceResult result = contentCategoryService.insert(contentCategory);
        String jsonResult = JsonUtils.objectToJson(result);
        return jsonResult;
    }

    @RequestMapping(value = "contentCategory", method = RequestMethod.DELETE, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(TbContentCategory contentCategory) {
        ZShop4jResponceResult result = contentCategoryService.delete(contentCategory);
        String jsonData = JsonUtils.objectToJson(result);
        return jsonData;
    }

    @RequestMapping(value = "contentCategory", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(TbContentCategory contentCategory) {
        ZShop4jResponceResult result = contentCategoryService.update(contentCategory);
        String jsonData = JsonUtils.objectToJson(result);
        return jsonData;
    }
}
